package com.xd.cloud.web;

import com.xd.cloud.common.bean.MessageBean;
import com.xd.cloud.entity.core.Role;
import com.xd.cloud.entity.core.User;
import com.xd.cloud.entity.core.UserRole;
import com.xd.cloud.service.core.RoleService;
import com.xd.cloud.service.core.UserRoleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.persistence.criteria.CriteriaBuilder;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author wangtao
 * @class_name Rolecontroller
 * @description 角色控制层
 * @create 2017/12/19
 */
@Controller
public class Rolecontroller extends BaseController {
    @Autowired
    private RoleService roleService;
    @Autowired
    private UserRoleService userRoleService;
    @RequestMapping(value = "/role/{id}",method = RequestMethod.GET,headers = "Accept=application/json")
    @ResponseBody
    public Object getRolebyId(HttpServletRequest request, HttpServletResponse response, @PathVariable() Integer id){
        Role role = roleService.get(id);
        return new MessageBean(true,role);
    }

    @RequestMapping(value = "/role/",method = RequestMethod.GET,headers = "Accept=application/json")
    @ResponseBody
    public Object getAllRole(HttpServletRequest request, HttpServletResponse response, @RequestParam Integer userid){
        List<UserRole> byuseridNoStatus = userRoleService.getByuseridNoStatus(userid);
        UserRole userRole = null;
        if(byuseridNoStatus.size() >0){
            userRole = byuseridNoStatus.get(0);
        }
        List<Role> role = roleService.getAll();
        Map map = new HashMap<String,Object>();
        map.put("role",role);
        map.put("userRole",userRole);
        return new MessageBean(true,map);
    }
}
